/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hookedonplay.decoviewsample;

import com.hookedonplay.decoviewlib.DecoView;
import com.hookedonplay.decoviewlib.LogUtil;
import com.hookedonplay.decoviewlib.PointF;
import com.hookedonplay.decoviewlib.charts.DecoDrawEffect;
import com.hookedonplay.decoviewlib.charts.SeriesItem;
import com.hookedonplay.decoviewlib.events.DecoEvent;
import com.hookedonplay.decoviewlib.interpolator.AccelerateInterpolator;
import com.hookedonplay.decoviewlib.interpolator.LinearInterpolator;
import com.hookedonplay.decoviewlib.interpolator.OvershootInterpolator;
import com.hookedonplay.decoviewsample.ResourceTable;
import com.hookedonplay.decoviewsample.Utility;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;

public class DecoAbility extends Ability {

    final private int[] mPalette = {
            RgbPalette.parse("#f57c00"),
            RgbPalette.parse("#212121"),
            RgbPalette.parse("#4caf50"),
            RgbPalette.parse("#727272"),
            RgbPalette.parse("#b6b6b6")};

    private int mSeries1Index;

    private DecoView arcView;

    private boolean createAnimation() {
        createTracks();
        setupEvents();
        return false;
    }

    private void createTracks() {
        arcView.deleteAll();
        arcView.configureAngles(270, 90);

        final int count = mPalette.length;
        final float width = 28;

        for (int i = 0; i < count; i++) {
            float inset = i * Utility.getDimension(width - 5, getApplicationContext());
            SeriesItem seriesItem1 = new SeriesItem.Builder(mPalette[i])
                    .setRange(0, 100, 0)
                    .setLineWidth(Utility.getDimension(width, getApplicationContext()))
                    .setInset(new PointF(inset, inset))
                    .setShowPointWhenEmpty(false)
                    .build();

            mSeries1Index = arcView.addSeries(seriesItem1);
        }
    }

    private void setupEvents() {
        int count = mPalette.length;
        for (int i = 0; i < count; i++) {
            DecoEvent revealEvent = new DecoEvent.Builder(DecoDrawEffect.EffectType.EFFECT_SPIRAL_OUT)
                    .setIndex(mSeries1Index - i)
                    .setDelay(200 * i)
                    .setDuration(1500)
                    .build();

            arcView.addEvent(revealEvent);

            arcView.addEvent(new DecoEvent.Builder(100).setIndex(mSeries1Index - i).setDelay(1500 + (200 * i))
                    .setInterpolator(new OvershootInterpolator())
                    .setDuration(4000).build());

            arcView.addEvent(new DecoEvent.Builder(0).setIndex(mSeries1Index - i).setDelay(5750 + (200 * i))
                    .setInterpolator(new AccelerateInterpolator())
                    .setDuration(1500).build());

            arcView.addEvent(new DecoEvent.Builder(DecoDrawEffect.EffectType.EFFECT_SPIRAL_IN)
                    .setIndex(mSeries1Index - i)
                    .setEffectRotations(3)
                    .setDelay(7250 + (200 * i))
                    .setDuration(1500)
                    .setInterpolator(new LinearInterpolator())
                    .build());
        }
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_deco);

        arcView = (DecoView) findComponentById(ResourceTable.Id_decoView);
    }

    @Override
    public void onActive() {
        super.onActive();

        createAnimation();
    }
}